package phone.qyg.com.qyg.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.app.adapter.recycler.RecycleViewDivider;
import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.PromptManager;
import com.app.view.BaseViewStateLayout;
import com.liaoinstan.springview.container.AliFooter;
import com.liaoinstan.springview.container.AliHeader;
import com.liaoinstan.springview.widget.SpringView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.adapter.DiscussAdapter;
import phone.qyg.com.qyg.model.ActivityDetail;
import phone.qyg.com.qyg.model.Discuss;
import phone.qyg.com.qyg.model.VenueDetail;
import phone.qyg.com.qyg.utils.AppHttpRequest;

/**
 * 作者：lblbh on 2017/12/8 17:36
 * 邮箱：lanbuhan@163.com
 * 功能：讨论列表
 */
@ContentView(R.layout.discuss)
public class DiscussActivity extends BaseActivity {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.springview)
    SpringView springview;

    @ViewInject(R.id.linear)
    ScrollView linear;

    BaseViewStateLayout baseView;

    DiscussAdapter adapter;

    RecyclerView recycler;

    List models;

    boolean isForce;

    int page_no,totalpage;

    int id,type;

    @Override
    public void initSucessView() {
        Object obj = getTransModels();
        if(obj instanceof ActivityDetail){
            ActivityDetail actDetail = (ActivityDetail) obj;
            id = actDetail.id;
            type = 1;
        }else if(obj instanceof VenueDetail){
            VenueDetail venueDetail = (VenueDetail) obj;
            id = venueDetail.id;
            type = 0;
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        baseView = new BaseViewStateLayout(QXApplication.getContext()) {

            @Override
            public View obtionView() {
                View sucessView = View.inflate(QXApplication.getContext(), R.layout.base_recycler,null);
                recycler = (RecyclerView) sucessView.findViewById(R.id.recycler);
                return sucessView;
            }

            @Override
            public Object obtionData() {
                getInitNetwork(false);
                return DELFAUTSTATE;
            }
        };

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        linear.addView(baseView,layoutParams);
        x.view().inject(linear);

        springview.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                isForce = true;
                getInitNetwork(false);
            }

            @Override
            public void onLoadmore() {
                if(isRequestList(models) && (page_no < totalpage) ){
                    getInitNetwork(true);
                }else{
                    PromptManager.showToast(R.string.more_no_datas);
                    springview.onFinishFreshAndLoad();
                }

            }
        });

        springview.setHeader(new AliHeader(this,false));
        springview.setFooter(new AliFooter(this,false));
    }


    /**
     * 执行网络请求
     */
    private void getInitNetwork(final boolean isPull){


        if(isPull){
            page_no = page_no + 1;
        }else{
            page_no = 1;
        }

        if(isForce){
            page_no = 1;
        }

        AppHttpRequest.getDiscussList(id,type, page_no, new AppHttpRequest.OnAppHttpListener() {
            @Override
            public void onAppHttpState(boolean isState, String result) {
                springview.onFinishFreshAndLoad();
                if(isState){
                    if(JSONTool.isStatus(result)){

                        List<Discuss> temps = JSONTool.jsonDefaluTranClazzs(result,JSONTool.Enum.COMMENT_LIST, Discuss.class);

                        totalpage = JSONTool.requestJSONfindNameCount(result,JSONTool.Enum.DATA,JSONTool.Enum.TOTALPAGE);

                        if(!isRequestList(temps) && !isPull){
                            baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_EMPTY);
                            baseView.stateView();
                            return;
                        }
                        //成功获取到数据
                        baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_SUCESS);
                        baseView.stateView();

                        setDiscussData(isPull,temps);
                    }else{
                        PromptManager.showToast(JSONTool.errorHint(result));
                    }
                }else{
                    PromptManager.showToast(R.string.service_error);
                }
            }
        });
    }


    private void setDiscussData(final boolean isPull, List<Discuss> temps) {
        if ((adapter == null && isRequestList(temps)) || isForce) {
            isForce = false;

            //数据初始化工作
            models = temps;

            adapter = new DiscussAdapter(QXApplication.getContext(),models,(type == 1)? ApplicationConfig.ACT:ApplicationConfig.VENUE);


            recycler.setAdapter(adapter);

        }

        if (isPull && isRequestList(models) && isRequestList(temps)) {
            //上拉加载数据
            models.addAll(temps);
        }

//        //刷新数据
        if (adapter != null)
            adapter.notifyDataSetChanged();
    }

    @Event(value = {R.id.back})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
        }
    }

    @Override
    public void initViewHandler() {
        if(null == recycler) return;
        LinearLayoutManager layoutVerManager = new LinearLayoutManager(QXApplication.getContext());
        layoutVerManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL,
                CommonUtil.dip2px(QXApplication.getContext(),1),
                getResources().getColor(R.color.divider)));
        recycler.setLayoutManager(layoutVerManager);
    }
}
